package com.zhanglifan.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2016/9/26 8:59
 * Description: CustomerCredentialMacher
 */
public class CustomerCredentialMacher extends SimpleCredentialsMatcher {

    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {


//        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
//
//        String clientPassword = Encrypt.md5(new String(upToken.getPassword()),upToken.getUsername());
//
//        System.out.println("*** class 的值是: " + info.getCredentials().getClass() + " ***");
//
//        return this.equals(clientPassword,info.getCredentials());

        System.out.println("调用了密码比较器");

        UsernamePasswordToken userToken = (UsernamePasswordToken) token;

        String pass = new String(userToken.getPassword());

        //1.用户输入密码    2.用户输入的用户名   3.hash几次，默认推荐2次
        Md5Hash hash = new Md5Hash(pass, userToken.getUsername(), 2);

        //进行密码的比较，如果正确返回true，否则返回false
        return equals(hash.toString(), info.getCredentials());
    }
}
